<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					icon="el-icon-star-filled"
					type="primary"
					:disabled="selection.length == 0"
					@click="top(1)"
					>推荐</el-button
				>
				<el-button
					icon="el-icon-star"
					:disabled="selection.length == 0"
					type="info"
					@click="top(2)"
					>取消推荐</el-button
				>
				<el-button
					icon="el-icon-select"
					type="primary"
					:disabled="selection.length == 0"
					@click="audit(1)"
					>审核通过</el-button
				>
				<el-button
					icon="el-icon-close"
					:disabled="selection.length == 0"
					type="danger"
					@click="audit(2)"
					>审核不通过</el-button
				>
				<el-button
					icon="el-icon-delete"
					plain
					type="danger"
					:disabled="selection.length == 0"
					@click="batch_del"
				/>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input
						v-model="param.key"
						clearable
						placeholder="关键字"
					/>
					<el-button
						icon="el-icon-search"
						type="primary"
						@click="search"
					/>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:api-obj="apiObj"
				:column="column"
				row-key="id"
				@menu-handle="menuHandle"
				@selection-change="selectionChange"
			>
				<!-- 固定列-选择列 -->
				<el-table-column fixed type="selection" width="60" />
				<el-table-column
					align="center"
					fixed="right"
					label="操作"
					width="170"
				>
					<template #default="scope">
						<el-button
							size="small"
							type="text"
							@click="open_dialog(scope.row)"
						>
							奖励勋章
						</el-button>
						<el-divider direction="vertical" />
						<el-popconfirm
							title="确定删除吗？"
							@confirm="table_del(scope.row, scope.$index)"
						>
							<template #reference>
								<el-button type="text" size="small"
									>删除</el-button
								>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
				<template #status="{ data }">
					<el-tag
						disable-transitions
						:type="data.status ? 'success' : 'danger'"
					>
						{{ data.status ? "已审核" : "未审核" }}
					</el-tag>
				</template>
				<template #isTop="{ data }">
					<el-tag
						disable-transitions
						:type="data.isTop ? 'success' : 'info'"
					>
						{{ data.isTop ? "推荐" : "未推荐" }}
					</el-tag>
				</template>
				<template #user="{ data }">
					<el-avatar
						class="user-avatar"
						:src="resHead(data.user)"
					/>{{ data.user.nickName }}
				</template>
				<template #imageArr="{ data }">
					<el-tag
						v-if="data.imageArr.length == 0"
						disable-transitions
						effect="dark"
					>
						{{ data.imageArr?.length }}
					</el-tag>
					<el-image
						v-if="data.imageArr.length > 0"
						style="width: 60px; height: 60px"
						:src="data.imageArr[0]"
						:preview-src-list="data.imageArr"
						:initial-index="0"
						fit="cover"
						shape="square"
						preview-teleported
					/>
				</template>
				<template #title="{ data }">
					<p v-if="data.title">标题：{{ data.title }}</p>
					<p>内容：{{ data.summary }}</p>
				</template>
			</scTable>
		</el-main>
		<modify ref="modify" @complete="complete" />
	</el-container>
</template>
<script>
import { defineAsyncComponent } from "vue";
export default {
	components: {
		modify: defineAsyncComponent(() => import("./modify")),
	},
	data() {
		return {
			apiObj: this.$API.cmcommunity.page,
			list: [],
			param: {
				key: "",
			},
			selection: [],
			column: [
				{
					label: "id",
					prop: "id",
					width: "200",
					sortable: true,
					hide: true,
				},
				{ prop: "user", label: "发表人", width: 180, align: "left" },
				{
					prop: "title",
					label: "发表内容",
					width: 260,
					showOverflowTooltip: true,
					align: "left",
				},
				{ prop: "status", label: "审核状态", width: 100 },
				{ prop: "isTop", label: "是否推荐", width: 100 },
				{ prop: "imageArr", label: "图片列表", width: 100 },
				{ prop: "praise", label: "点赞数量", width: 100 },
				{ prop: "comment", label: "评论数量", width: 100 },
				{ prop: "share", label: "分享数量", width: 100 },
				{
					prop: "createTime",
					label: "创建时间",
					width: 180,
				},
			],
		};
	},
	mounted() {},
	methods: {
		complete() {
			this.$refs.table.refresh();
		},
		search() {
			this.$refs.table.upData(this.param);
		},
		resHead(data) {
			if (data.headPic && data.headPic.indexOf("http") == -1) {
				data.headPic = this.$CONFIG.SERVER_URL + data.headPic;
			}
			return data.headPic;
		},
		audit(type) {
			this.$confirm(
				`确定执行` +
					(type == 1 ? "【审核通过】" : "【审核不通过】") +
					`选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}
			)
				.then(async () => {
					const loading = this.$loading();
					let ids = [];
					this.selection.forEach((element) => {
						ids.push(element.id);
					});
					var res = await this.$API.cmcommunity.audit.put({
						ids: ids,
						audit: type == 1 ? true : false,
					});
					if (res.code == 200) {
						this.$refs.table.refresh();
						loading.close();
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				})
				.catch(() => {});
		},
		top(type) {
			this.$confirm(
				`确定执行` +
					(type == 1 ? "【推荐】" : "【取消推荐】") +
					`选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}
			)
				.then(async () => {
					const loading = this.$loading();
					let ids = [];
					this.selection.forEach((element) => {
						ids.push(element.id);
					});
					var res = await this.$API.cmcommunity.top.put({
						ids: ids,
						audit: type == 1 ? true : false,
					});
					if (res.code == 200) {
						this.$refs.table.refresh();
						loading.close();
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				})
				.catch(() => {});
		},
		//删除
		async table_del(row) {
			var res = await this.$API.cmcommunity.delete.delete([row.id]);
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//批量删除
		batch_del() {
			this.$confirm(
				`确定删除选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}
			)
				.then(async () => {
					const loading = this.$loading();
					let ids = [];
					this.selection.forEach((element) => {
						ids.push(element.id);
					});
					var res = await this.$API.cmcommunity.delete.delete(ids);
					if (res.code == 200) {
						this.$refs.table.refresh();
						loading.close();
						this.$message.success("删除成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				})
				.catch(() => {});
		},
		open_dialog(row) {
			if (row.id) {
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open();
			}
		},
		selectionChange(selection) {
			this.selection = selection;
		},
		menuHandle(obj) {
			if (obj.command == "add") {
				this.open_dialog({});
			}
			if (obj.command == "edit") {
				this.open_dialog(obj.row);
			}
			if (obj.command == "delete") {
				this.table_del(obj.row);
			}
		},
	},
};
</script>
<style lang="scss" scoped>
.user-avatar {
	vertical-align: middle;
	margin-right: 5px;
}
</style>
